class ProfileController < ApplicationController
  layout 'layouts/standard_layout'
  def index
    @person = Person.find_by_username(session[:username])
  end

  def edit
    unless params[:id]
      @person = (Person.find_by_username(session[:username])||Person.new)
    else
      @person = Person.find(params[:id])
    end    
  end

  def save
    unless params[:person][:id].empty?
      @person = Person.find(params[:person][:id])
    else   
      unless params[:force]
        @person = Person.find_by_first_name_and_last_name(
          params[:person][:first_name], params[:person][:last_name]
        ) if params[:person][:first_name] && params[:person][:last_name]
        if @person
          flash[:notice] = "An existing profile exists with similar data, is this you?"
          session[:form_vars] = params
          redirect_to :action=>'confirm', :id=>@person.id
          return
        end
      end      
      @person = Person.new
    end    
    @person.username = params[:person][:username]
    @person.first_name = params[:person][:first_name]
    @person.middle_name = params[:person][:middle_name]
    @person.last_name = params[:person][:last_name]
    @person.email = params[:person][:email]
    @person.organization = params[:person][:organization]
    @person.title = params[:person][:title]
    @person.save          
  end

  def confirm
    @person = Person.find(params[:id])
  end
end
